﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DHQGHN.DAL.Base;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.Models.Admin;

namespace DHQGHN.DAL.DAO.Admin
{
    public class MenuInRoleDAO : SqlDAO
    {
        public const string SP_GET_LIST_PAGING_MENU_IN_ROLE = "spGetListPagingMenuInRole";
        public const string SP_GET_LIST_PAGING_MENU_IN_ROLE_BY_ID = "spGetMenuInRoleById";
        public const string SP_GET_LIST_MENU_OF_ROLE = "spGetListMenuInRoleByRoleID";
        public const string SP_CREATE_MENU_IN_ROLE = "spCreateOrUpdateMenuInRole";
        public const string SP_DELETE_MENU_IN_ROLE = "spDeleteMenuInRole";
        public const string Param_MenuInRoleID = "@MenuInRoleID";
        public const string Param_MenuID = "@MenuID";
        public const string Param_RoleID = "@RoleID";
        public const string Param_RoleName = "@RoleName";
        public const string Param_AppID = "@AppID";

        public MenuInRoleDAO(String connectionString, System.Type type = null)
            : base(connectionString, type)
        {

        }

        internal DataPage<MenuInRoleModel> GetDynamicListPaging(MenuInRoleModel model, int pageSize, int pageIndex, Dictionary<String, String> mapDictionary, bool compareEqual)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_LIST_PAGING_MENU_IN_ROLE);
                if (model != null)
                {
                    command.Parameters.Add(new SqlParameter(Param_MenuInRoleID, model.MenuInRoleID));
                    command.Parameters.Add(new SqlParameter(Param_MenuID, model.MenuID));
                    command.Parameters.Add(new SqlParameter(Param_RoleID, model.RoleID));
                    command.Parameters.Add(new SqlParameter(Param_AppID, model.AppID));
                }
                command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
                command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
                return GetDataPage<MenuInRoleModel>(command, pageIndex, pageSize, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal MenuInRoleModel GetById(int? ID, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_LIST_PAGING_MENU_IN_ROLE_BY_ID);
                command.Parameters.Add(new SqlParameter(Param_MenuInRoleID, ID));

                return GetSingle<MenuInRoleModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal List<MenuInRoleModel> GetListMenuInRoleByRoleID(int? roleID, int? appID, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_LIST_MENU_OF_ROLE);
                command.Parameters.Add(new SqlParameter(Param_RoleID, roleID));
                command.Parameters.Add(CreateParameter(Param_AppID,appID));

                return GetList<MenuInRoleModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal bool createMenuInRole(MenuInRoleModel model, int[] lstMenuID)
        {
            try
            {
                foreach (int menuID in lstMenuID)
                {
                    SqlCommand command = this.GetSprocCommand(SP_CREATE_MENU_IN_ROLE);
                    command.Parameters.Add(new SqlParameter("@" + MenuInRoleModel.FIELD_MENU_IN_ROLE_ID, model.MenuInRoleID));
                    command.Parameters.Add(new SqlParameter("@" + MenuInRoleModel.FIELD_MENU_ID, menuID));
                    command.Parameters.Add(new SqlParameter("@" + MenuInRoleModel.FIELD_ROLE_ID, model.RoleID));
                    command.Parameters.Add(new SqlParameter("@" + MenuInRoleModel.FIELD_DESCRIPTION, model.Descriptions));
                    if (ExecuteQuery(command) <= 0)
                    {
                        return false;
                    }
                }
                return true;
            }
            catch
            {
                throw;
            }
        }

        internal bool deleteMenuInRole(int[] ID)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_DELETE_MENU_IN_ROLE);
                command.Parameters.Add(new SqlParameter("@MenuInRoleList", GetTableId<int>(ID, "MenuInRoleID")));
                int result = ExecuteQuery(command);

                return result > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }
    }
}
